home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / crobots.zip / SNIPER.R < prev   
Text File  |  1986-08-07  |  5KB  |  183 lines

  1.  
  2. /* sniper */
  3. /* strategy: since a scan of the entire battlefield can be done in 90 */
  4. /* degrees from a corner, sniper can scan the field quickly. */
  5.  
  6. /* external variables, that can be used by any function */
  7. int corner;           /* current corner 0, 1, 2, or 2 */
  8. int c1x, c1y;         /* corner 1 x and y */
  9. int c2x, c2y;         /*   "    2 "  "  " */
  10. int c3x, c3y;         /*   "    3 "  "  " */
  11. int c4x, c4y;         /*   "    4 "  "  " */
  12. int s1, s2, s3, s4;   /* starting scan position for corner 1 - 4 */
  13. int sc;               /* current scan start */
  14. int d;                /* last damage check */
  15.  
  16.  
  17.  
  18. /* main */
  19. main()
  20. {
  21.   int closest;        /* check for targets in range */
  22.   int range;          /* range to target */
  23.   int dir;            /* scan direction */
  24.  
  25.   /* initialize the corner info */
  26.   /* x and y location of a corner, and starting scan degree */
  27.   c1x = 10;  c1y = 10;  s1 = 0;
  28.   c2x = 10;  c2y = 990; s2 = 270;
  29.   c3x = 990; c3y = 990; s3 = 180;
  30.   c4x = 990; c4y = 10;  s4 = 90;
  31.   closest = 9999;
  32.   new_corner();       /* start at a random corner */ 
  33.   d = damage();       /* get current damage */
  34.   dir = sc;           /* starting scan direction */ 
  35.  
  36.   while (1) {         /* loop is executed forever */
  37.  
  38.     while (dir < sc + 90) {  /* scan through 90 degree range */
  39.       range = scan(dir,1);   /* look at a direction */
  40.       if (range <= 700 && range > 0) { 
  41.         while (range > 0) {    /* keep firing while in range */
  42.           closest = range;     /* set closest flag */
  43.           cannon(dir,range);   /* fire! */
  44.           range = scan(dir,1); /* check target again */
  45.           if (d + 15 > damage())  /* sustained several hits, */ 
  46.             range = 0;            /* goto new corner */
  47.         }
  48.         dir -= 10;             /* back up scan, in case */
  49.       }
  50.  
  51.       dir += 2;                /* increment scan */
  52.       if (d != damage()) {     /* check for damage incurred */
  53.         new_corner();          /* we're hit, move now */
  54.         d = damage();
  55.         dir = sc;
  56.       }
  57.     }
  58.  
  59.     if (closest == 9999) {       /* check for any targets in range */
  60.       new_corner();             /* nothing, move to new corner */
  61.       d = damage();
  62.       dir = sc;
  63.     } else                      /* targets in range, resume */
  64.       dir = sc;
  65.     closest = 9999;
  66.   } 
  67.  
  68. }  /* end of main */
  69.  
  70.  
  71.  
  72. /* new corner function to move to a different corner */
  73. new_corner() {
  74.   int x, y;
  75.   int angle;
  76.   int new;
  77.  
  78.   new = rand(4);           /* pick a random corner */
  79.   if (new == corner)       /* but make it different than the */
  80.     corner = (new + 1) % 4;/* current corner */
  81.   else
  82.     corner = new;
  83.   if (corner == 0) {       /* set new x,y and scan start */
  84.     x = c1x; 
  85.     y = c1y; 
  86.     sc = s1;
  87.   } 
  88.   if (corner == 1) {
  89.     x = c2x; 
  90.     y = c2y; 
  91.     sc = s2;
  92.   }
  93.   if (corner == 2) {
  94.     x = c3x; 
  95.     y = c3y; 
  96.     sc = s3;
  97.   }
  98.   if (corner == 3) { 
  99.     x = c4x; 
  100.     y = c4y; 
  101.     sc = s4;
  102.   }
  103.  
  104.   /* find the heading we need to get to the desired corner */
  105.   angle = plot_course(x,y);
  106.  
  107.   /* start drive train, full speed */
  108.   drive(angle,100);
  109.  
  110.   /* keep traveling until we are within 100 meters */
  111.   /* speed is checked in case we run into wall, other robot */
  112.   /* not terribly great, since were are doing nothing while moving */
  113.  
  114.   while (distance(loc_x(),loc_y(),x,y) > 100 && speed() > 0)
  115.     ;
  116.  
  117.   /* cut speed, and creep the rest of the way */
  118.  
  119.   drive(angle,20);
  120.   while (distance(loc_x(),loc_y(),x,y) > 10 && speed() > 0)
  121.     ;
  122.  
  123.   /* stop drive, should coast in the rest of the way */
  124.   drive(angle,0); 
  125. }  /* end of new_corner */
  126.  
  127. /* classical pythagorean distance formula */
  128. distance(x1,y1,x2,y2)
  129. int x1;
  130. int y1;
  131. int x2;
  132. int y2;
  133. {
  134.   int x, y;
  135.  
  136.   x = x1 - x2;
  137.   y = y1 - y2;
  138.   d = sqrt((x*x) + (y*y));
  139.   return(d);
  140. }
  141.  
  142. /* plot course function, return degree heading to */
  143. /* reach destination x, y; uses atan() trig function */
  144. plot_course(xx,yy)
  145. int xx, yy;
  146. {
  147.   int d;
  148.   int x,y;
  149.   int scale;
  150.   int curx, cury;
  151.  
  152.   scale = 100000;  /* scale for trig functions */
  153.   curx = loc_x();  /* get current location */
  154.   cury = loc_y();
  155.   x = curx - xx;
  156.   y = cury - yy;
  157.  
  158.   /* atan only returns -90 to +90, so figure out how to use */
  159.   /* the atan() value */
  160.  
  161.   if (x == 0) {      /* x is zero, we either move due north or south */
  162.     if (yy > cury)
  163.       d = 90;        /* north */
  164.     else
  165.       d = 270;       /* south */
  166.   } else {
  167.     if (yy < cury) {
  168.       if (xx > curx)
  169.         d = 360 + atan((scale * y) / x);  /* south-east, quadrant 4 */
  170.       else
  171.         d = 180 + atan((scale * y) / x);  /* south-west, quadrant 3 */ 
  172.     } else {
  173.       if (xx > curx)
  174.         d = atan((scale * y) / x);        /* north-east, quadrant 1 */
  175.       else
  176.         d = 180 + atan((scale * y) / x);  /* north-west, quadrant 2 */
  177.     }
  178.   }
  179.   return (d);
  180. }
  181.  
  182.  
  183.